import {Component, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core';
import {FormBuilder, FormControl, FormGroup} from '@angular/forms';
import {DeviceInfoService} from '../service/device-info.service';
import {ConditionExpr, EntityCondition, EntityOperator} from '@shared-utils/entity';
import {Observable, of} from 'rxjs';
import {HttpClient} from '@angular/common/http';
import {map} from 'rxjs/operators';
import {DatePipe} from '@angular/common';

@Component({
  selector: 'app-report',
  templateUrl: './report.component.html',
  styleUrls: ['./report.component.scss']
})
export class ReportComponent implements OnInit {
  @Input() reportChangeObserver: Observable<void>;
  validateForm!: FormGroup;
  controlArray: Array<{ index: number; show: boolean }> = [];

  queryData: any = {};
  submitQueryData = {
    entityName: 'DmzReport',
    viewSize: 10,
    viewIndex: 0,
    noConditionFind: 'Y',
    hasTimestamp: 'N',
    selectField: ['reportId', 'device', 'testTime', 'testUser', 'status', 'desc'],
    orderBy: [],
    condition: {conditionList: [], operator: EntityOperator.AND},
    fieldFormat: {}
  };
  pageData = {
    listSize: 0,
    pageChange: (event) => {
      this.submitQueryData.viewIndex = event - 1;
      this.loadDataList();
    }
  };
  dataList: Observable<Array<any>>;
  deviceList: Observable<any>;

  constructor(private fb: FormBuilder,
              public deviceInfoService: DeviceInfoService,
              public http: HttpClient) {
  }

  ngOnInit(): void {
    this.loadDataList();
    this.reportChangeObserver.subscribe(() => this.loadDataList());
  }

  loadDataList(): void {
    this.deviceList = this.deviceInfoService.getDeviceList();
    this.dataList = this.http.post('getPageData', this.submitQueryData).pipe(map((value: any) => {
      this.pageData.listSize = value.listSize;
      return value.list;
    }));
  }

  search(): void {

    const conditionList = [];
    if (this.queryData.device) {
      conditionList.push(new ConditionExpr('device', EntityOperator.EQUALS, this.queryData.device));
    }
    if (this.queryData.testTime) {
      const datePipe = new DatePipe('en');
      const startDate = datePipe.transform(this.queryData.testTime[0], 'yyyy-MM-dd');
      const endDate = datePipe.transform(this.queryData.testTime[1], 'yyyy-MM-dd');
      conditionList.push(new ConditionExpr('testTime', EntityOperator.GREATER_THAN_EQUAL_TO, startDate));
      conditionList.push(new ConditionExpr('testTime', EntityOperator.LESS_THAN_EQUAL_TO, endDate));
    }
    if ((this.queryData.testUser)) {
      conditionList.push(new ConditionExpr('testUser', EntityOperator.EQUALS, this.queryData.testUser));
    }
    if ((this.queryData.status)) {
      conditionList.push(new ConditionExpr('status', EntityOperator.EQUALS, this.queryData.status));
    }
    this.submitQueryData.condition.conditionList = conditionList;
    console.log('query', this.queryData);

    this.loadDataList();
  }

  getStatusColor(status: string): string {
    // '进行中','已完成','暂停中','已终止'
    let color;
    switch (status) {
      case '进行中':
        color = '#3333FF';
        break;
      case '已完成':
        color = '#55aa55';
        break;
      case '暂停中':
        color = '#FFDD66';
        break;
      case '已终止':
        color = '#EE3355';
        break;

    }
    return color;
  }

  viewReport(data: any): void {
    // window.open('reportInfo/' + data.reportId, '_blank', 'toolbar=yes,scrollbars=yes,resizable=yes');
    window.open('reportInfo/' + data.reportId);
  }
}
